package com.ciwong.epaper.modules.epaper.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.adapter.SelectQuestionAdapter;
import com.ciwong.epaper.modules.epaper.bean.ResourceDetail;
import com.ciwong.epaper.modules.epaper.bean.SwitchQuestionBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/17.
 */
public class SelectQuestionDialog extends Dialog {


    private ListView mLv;

    private Button submitBtn;

    private List<SwitchQuestionBean> mIsDoRecords;

    private SelectQuestionAdapter adapter;

    private OnSelectQuestionListener mOnSelectQuestionListener;

    public SelectQuestionDialog(Context context, List<ResourceDetail> isDoRecords) {
        super(context, R.style.ActionSheetDialogStyle);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_lsw_select_question);
        mIsDoRecords = new ArrayList<>();
        if (isDoRecords != null && isDoRecords.size() > 0) {
            for (ResourceDetail mResourceDetail : isDoRecords) {
                SwitchQuestionBean switchQuestionBean = new SwitchQuestionBean();
                switchQuestionBean.setComplected(mResourceDetail.isDo());
                switchQuestionBean.setButton(false);
                switchQuestionBean.setResourceDetail(mResourceDetail);
                mIsDoRecords.add(switchQuestionBean);
            }
        }
        SwitchQuestionBean completeButtonBean = new SwitchQuestionBean();
        completeButtonBean.setButton(true);
        mIsDoRecords.add(completeButtonBean);
        mLv = (ListView) findViewById(R.id.select_ques_lv);
        adapter = new SelectQuestionAdapter((Activity) context, mIsDoRecords);
        mLv.setAdapter(adapter);
        init();
    }


    private void init() {
        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Log.e("TAG", "onItemClick: ");
                SwitchQuestionBean switchQuestionBean = mIsDoRecords.get(position);
                if (switchQuestionBean.isButton()) {
                    mOnSelectQuestionListener.onSelectQuestion(0, true);
                } else {
                    boolean isDone = switchQuestionBean.isComplected();
//                    int index = switchQuestionBean.getResourceDetail().getIndex();
                    if (!isDone && null != mOnSelectQuestionListener) {
                        mOnSelectQuestionListener.onSelectQuestion(position, false);
                    }
                }
                dismiss();
            }
        });
    }


    public void setOnSelectQuestionListener(OnSelectQuestionListener listener) {

        mOnSelectQuestionListener = listener;
    }

    public interface OnSelectQuestionListener {
        void onSelectQuestion(int index, boolean isCommit);
    }

}
